/**
 * Project  : Looker
 * Package  : net.freestu.looker.impl
 * File     : MysqlMediaListener.java
 * Author   : solosky < solosky772@qq.com >
 * Date     : 2009-9-5
 * Modified : 2009-9-5
 * License  : Apache License 2.0 
 */
package net.freestu.looker.impl;

import java.sql.SQLException;
import java.util.Calendar;

import org.apache.commons.configuration.Configuration;

import net.freestu.looker.common.ListenerException;
import net.freestu.looker.common.Media;
import net.freestu.looker.common.MediaListener;
import net.freestu.looker.util.FileUtils;
import net.freestu.looker.util.MysqlQuery;
import net.freestu.looker.util.URLUtils;

/**
 *  把附件数据保存到MySQL
 */
public class MysqlMediaListener implements MediaListener
{

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.MediaListener#add(net.freestu.looker.common.Media)
	 */
	@Override
	public void add(Media media) throws ListenerException
	{
		StringBuffer sql = new StringBuffer();
		sql.append("INSERT INTO #media");
		sql.append(" (`mediahash` ,`newshash` ,`name` , `ext`,`savedir`,`type` ,`size` ,`url`, `date` ,`download` )");
		sql.append(" VALUES (");
		sql.append(""+media.getHash()+", ");
		sql.append(""+media.getNewsEntity().getHash()+", ");
		sql.append("'"+media.getName()+"', ");
		sql.append("'"+FileUtils.extension(media.getTmpFile().getName())+"', ");
		sql.append("'"+getSaveDir(media)+"', ");
		sql.append("'"+media.getType().name()+"', ");
		sql.append("'"+media.getSize()+"', ");
		sql.append("'"+media.getUrl()+"', ");
		sql.append(media.getDate().getTime()+",0); ");
		
		try {
	        MysqlQuery.getInstance().execute(sql.toString());
        } catch (SQLException e) {
        	throw new ListenerException("附件信息写入数据库时出错："+e.getMessage());
        }
	
	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.MediaListener#close()
	 */
	@Override
	public void close() throws ListenerException
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.MediaListener#commit()
	 */
	@Override
	public void commit() throws ListenerException
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.MediaListener#delete(net.freestu.looker.common.Media)
	 */
	@Override
	public void delete(Media media) throws ListenerException
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.MediaListener#init(org.apache.commons.configuration.Configuration)
	 */
	@Override
	public void init(Configuration conf) throws ListenerException
	{
		// TODO Auto-generated method stub

	}

	/* (non-Javadoc)
	 * @see net.freestu.looker.common.MediaListener#update(net.freestu.looker.common.Media)
	 */
	@Override
	public void update(Media media) throws ListenerException
	{
		// TODO Auto-generated method stub

	}
	
	/**
	 * 返回保存文件目录
	 * 文件保存规则：
	 * media.saveDir/domainName/4位数年-2位数月份/临时文件名
	 * @return String
	 * @throws ListenerException 
	 */
	private String getSaveDir(Media media)
	{
		StringBuffer saveDir = new StringBuffer();
		saveDir.append(URLUtils.domain(media.getUrl()));
		saveDir.append('/');
		//www.domain.com
		Calendar cl = Calendar.getInstance();
		saveDir.append(cl.get(Calendar.YEAR));
		saveDir.append("-");
		saveDir.append(cl.get(Calendar.MONTH));
		//www.domain.comm/2009-6/
		saveDir.append('/'+media.getType().name());
		//www.domain.com/2009-6/i/
		saveDir.append('/');
		return saveDir.toString();
	
	}

}
